TCL Scripting For VLSI Part 1

Part1: What is TCL?
  • It stands for Tool Command Language
  • Tcl is interpreter based
  • To interpreter a Tcl script you will require a Tcl Shell - tclsh
  • Tcl interpreter is a library of C procedures that can be incorporated in the applications.
  • Tk is one of the extensions of Tcl which is a ToolKit for creating GUI(Graphical User Interface) based applications
  • Tk extends the core Tcl facilities with commands for building user interfaces
  • Tk like Tcl is also implemented as a library of C procedures so it can be used in many different applications
  • Individual applications can also extend the base Tk features with new user-interface widgets (GUI Components like button, labels, textbox etc) and geometry managers written in C
Advantages of TCL
  • Tcl gives the benefit of rapid development
  • Many interesting GUI applications can be written entirely as Tcl scripts using a windowing shell called 'wish'.
  • When wish is used one can generate and execute new scripts on the fly without recompiling the application.
  • Easy and fast programming.
Features of TCL
  • It provides generic programming facilities such as variables, loops and procedures
  • Tcl Working: Tcl just does grouping, substitutions and command invocations
  • Write once, run anywhere. It runs on Windows, Mac OS X, and almost on every Unix platform
  • It's an open source, free, and can be used for commercial applications without any limit
A sample TCL Script
  • A script file is a list of commands which a shell interpreter read and execute.
Example: set a 19
                put $a
Comments
  • # is the character used to comment a statement
  • # and all the characters following it up through the next newline are treated as a comment and discarded
  • # mark can occur in a position where Tcl is expecting the first character of a command
Example: A script with a comment  
                #This is my first tcl script
                set a 19;
                puts $a
Executing a Tcl Script
There are two ways of how you can execute a Tcl Script
Method 1:
  • Invoke the tclsh command on the command prompt to see the tclsh prompt (%)
  • Type your Tcl commands that you want to execute.
  • Type 'exit' command on the prompt to exit from the shell.
Method 2:
  • Type your tcl program in a notepad/text editor and save it. (Conventionally tcl scripts will have a .tcl extension). Eg: script1.tcl
  • Execute your script by invoking the Tcl Shell (tclsh) C:\ tclsh script1.tclP
Part2: Commands and Variables
Script, commands and Words
  • The Tcl script comprises of a set of commands
  • Each command in turn consists of one or more words
  • The first word is the name of the command and additional words are arguments to that command
  • Words are separated by spaces or tabs 
Evaluating a command
  • Tcl interpreter evaluates a command in two steps - parsing and execution.
First Step: Parsing
  • First step during command evaluation
  • Chops commands into words
  • Perform substitutions
  • Does not interpret values of words.
  • Certain rules are applied while performing substitutions.
Second Step: Execution
  • In the execution step, meaning is applied to the words of the command
  • Tcl treats the first word as the command name
  • The rest of the words are treated as the arguments to the command
  • A command procedure is located and invoked to carry out the command's functionality
Substitution
Tcl provides three forms of substitution they are :
  • Variable substitution
  • Command substitution
  • Backslash substitution
Substitutions can occur in any word of the command
There may be any number of substitutions within a single word
Variable substitution
  • Variable substitution is achieved by a preceding the variable name with a dollar sign character. Example: $x
  • It causes the value of the Tcl variable to be inserted into a word
Example:
Command substitution
  • Command substitution causes the result of a Tcl command to be substituted
  • Command substitution is invoked by enclosing the command in brackets (Square brackets [])
Example:

Backslash substitution
  • It is used to substitute the values of certain special characters preceded by a '\' character
  • Example: \n (newline), \t (tab)
        #!/usr/bin/tclsh
        puts "Hello\nWorld"  
  • When the above code is executed, it produces the following result −
      - Output: Hello
                     World
Grouping
  • Space is a word separator hence a space, tab or a newline cannot be part of the argument
  • However it can be made as part of the argument by grouping it together with the arguments with the help of quotes
  • There are 2 ways of grouping words in tcl,
            -With double quotes - "  "
            - Curly braces - { }
Grouping with double quotes
  • Double quotes disable word and command separator
  • If a word is enclosed in double quotes, then spaces, tabs, newlines and semicolons are treated as ordinary characters within the word
  • Variable, command, backslash substitutions all occur as usual inside double quotes
Example:

Grouping with curly braces
  • Within curly braces all the special characters lose their meaning
  • Spaces, newlines and semicolons are treated as ordinary characters
  • No substitutions are performed on the word
Example:
Variables
  • Variables are containers to hold values
  • In Tcl the following are some pointers on the variables:
        -Length of the variable name is not limited
        -Variable names are case sensitive
        -They need not be pre - declared as Tcl is a weakly typed language
        -No special character can be a part of the variable name
        -In Tcl every value is treated as a string
        -Both the name and value of the variable can be arbitrary strings of characters
The set command
  • The set command is used for assigning value to a variable. The syntax for set command is,
         set variableName value
  • Variables may be created, read and modified with the set command
  • The set commands takes either two arguments or one argument
Example: 

Predefined variables
  • Some important pre-defined variables in Tcl are:
    -argv0 simple variable that holds the name of the script being executed
    - argv: simple variable that holds the command line arguments
    - argc: simple variable that holds the count of the command line arguments
    -env: array variable that holds the process's environment variables
SHARE

vlsi4freshers

Hi I’m Designer of this blog.

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment